package controller

import (
	"github.com/kataras/iris/mvc"
	"github.com/kataras/iris"
	"cn.pinming/gencode/core/controller"
	"cn.pinming/gencode/core/api"
	"cn.pinming/gencode/modules/sys/vo"
	"cn.pinming/gencode/core"
)

type LoginController struct {
	controller.BaseController
}

func (this *LoginController) GetLogin() mvc.Result  {
	return this.ViewResult("login");
}

func (this *LoginController) GetUserError() mvc.Result  {
	panic("系统错误500测试")
	return this.ViewResult("login");
}

func (this *LoginController) PostLogin(ctx iris.Context) api.Result  {
	request:=new(vo.LoginRequest )
	ctx.ReadJSON(request)
	if(request.Username!="admin"){
		return this.Error("用户名密码无效")
	}
	if(len(request.Password)<2){
		return this.Error("用户名密码无效")
	}
	user:= core.Userinfo{
		Uid:1,
		Username:request.Username,
	}
	core.SetCurrentUser(this.Session,user)
	return this.Success();
}

func (this *LoginController) GetLogout()  {
	core.AbandonSession(this.Session)
	this.Ctx.Redirect("/login")
}